class IUP_PROGRESS_DIALOG
-- Creates a progress dialog element. It is a predefined dialog for displaying 
-- the progress of an operation. The dialog is meant to be shown with the show 
-- functions show or show_xy.
--
-- The IUP_PROGRESS_DIALOG will display the iteration state using a 
-- IUP_PROGRESS_BAR control.
--
-- The dialog is not automatically closed, the application must do that 
-- manually inside the CANCEL_CB callback or inside your processing loop by 
-- checking the STATE attribute.
	
inherit
	IUP_DIALOG
		redefine
			execute_cancel
		end
	IUP_WIDGET_CUSTOM_ATTRIBUTES
		export
			{NONE} all
		end
	
create {ANY}
  progress_dialog
	
feature {ANY}

	progress_dialog
		local
			a_progress_dialog: POINTER
		do
			a_progress_dialog := int_progress_dialog

			set_widget(a_progress_dialog)
		end

	-- Operations

	exit_loop
		-- Close the loop.
		do
			iup_open.exit_loop
		end

	-- Attributes

	set_count (value: INTEGER)
	-- Set current count of iterations.
		do
			iup_open.set_attribute(Current, "COUNT", value.out)
		end

	get_count: INTEGER
		-- The current count of iterations.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "COUNT")

			if str.is_integer then
				Result := str.to_integer
			end
		end

	increment (value: INTEGER)
		-- (write-only): increment the current count by the given amount.
		do
			iup_open.set_attribute(Current, "INC", value.out)
		end

	increment_one
		-- (write-only): increment the current count by 1.
		do
			iup_open.set_attribute(Current, "INC", "1")
		end

	set_percent (value: INTEGER)
		-- Set current percent of iterations.
		do
			iup_open.set_attribute(Current, "PERCENT", value.out)	
		end

	get_percent: INTEGER
			-- The current percent of iterations.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "PERCENT")

			if str.is_integer then
				Result := str.to_integer
			end
		end

	set_total_count (value: INTEGER)
		-- Set the total number of iterations. 
		do
			iup_open.set_attribute(Current, "TOTALCOUNT", value.out)	
		end

	set_state (state: STRING)
		-- Describe the state of the iteration. Can be: IDLE, PROCESSING, 
		-- UNDEFINED or ABORTED. Default is IDLE. When INC, COUNT or PERCENT are 
		-- set the state is changed to PROCESSING. If the user pressed the Cancel 
		-- button the state is changed to ABORTED, but check the CANCEL_CB 
		-- callback for other options. If the state is set to UNDEFINED by the 
		-- application the progress bar will display an undefined state animation 
		-- (same as setting MARQUEE=True in IUP_PROGRESS_BAR), to resume 
		-- processing set the state attribute to PROCESSING.
		require
			is_valid_state(state)
		do
			iup_open.set_attribute(Current, "STATE", state)
		end

	get_state: STRING
		-- The current state.
		do
			Result := iup_open.get_attribute(Current, "STATE")
		end

	set_description (text: STRING)
		-- Text description to be shown at the dialog.
		do
			iup_open.set_attribute(Current, "DESCRIPTION", text)
		end

	set_progress_bar_height (height: INTEGER)
		-- Height in pixels of the progress bar. Default: 30
		require
			height > 0
		do
			iup_open.set_attribute(Current, "PROGRESSHEIGHT", height.out)
		end

	set_minimun_clock (value: INTEGER)
		-- Minimum time between counts to an actual screen update in 
		-- milliseconds. So if each count increment is too fast this avoids 
		-- updating the screen too often. Default: 250
		require
			value > 0
		do
			iup_open.set_attribute(Current, "MINCLOCK", value.out)
		end

	set_minimun_percent (value: INTEGER)
		-- Minimum percent between counts to an actual screen update. So if each 
		-- count increment is too fast and the minimum clock has not reached yet, 
		-- but the application would like to update the screen anyway after the 
		-- percent of the total progress has passed this minimum value. So at 
		-- least some screen updates will occur. Default: 10
		require
			value > 0
		do
			iup_open.set_attribute(Current, "MINPERCENT", value.out)
		end

	-- Callback

	set_cb_cancel (act: detachable FUNCTION[TUPLE[IUP_PROGRESS_DIALOG], STRING])
		-- Action generated when the user clicked on the Cancel button.
		-- Returns: if different from IUP_CONTINUE then STATE will be changed to 
		-- ABORTED, same behavior as if the callback does not exist.
		local
			operation: INTEGER
		do
			cb_close_dialog := act
			
			if cb_close_dialog /= Void then
				operation := 1
			else
				operation := 0
			end
						
			iup_open.set_callback (Current, "CLOSE_CB", "NONEEDED", operation)
		end

	-- Validations

	is_valid_state (value: STRING): BOOLEAN
		do
			if value.is_equal("IDLE") or
				value.is_equal("PROCESSING") or
				value.is_equal("UNDEFINED") or
				value.is_equal("ABORTED") then
				Result := True
			else
				Result := False
			end
		end

feature {IUP}

	execute_cancel: STRING
		do
			if attached cb_close_dialog as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

feature {NONE}

	-- Callback
	cb_close_dialog: detachable FUNCTION[TUPLE[IUP_PROGRESS_DIALOG], STRING]

	-- Internal

	int_progress_dialog: POINTER
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"return IupProgressDlg ();"
      end

end

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017, 2019, 2020 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
